package com.jt.demo1.proxy;

import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;

public class JDKProxy {
    public static Object getProxy(Object target){
        //1.类加载器
        ClassLoader classLoader = target.getClass().getClassLoader();
        //2.获取数组对象
        Class<?>[] interfaces = target.getClass().getInterfaces();
        //.获取处理对象
        InvocationHandler invocationHandler = getInvocationHandler(target);
        //.创建对象
        return Proxy.newProxyInstance(classLoader,interfaces,invocationHandler);
    }
    public static InvocationHandler getInvocationHandler (Object target){
             return new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] args) throws Throwable {
                long l = System.nanoTime();
                System.out.println("事物开始");
                Object result =method.invoke(target, args);
                System.out.println("事物结束");
                long l1 = System.nanoTime();
                System.out.println(";"+(l1-l));
                return result;
            }
        };

    }
}
